package query.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import query.bean.Questionnaire;
import query.bean.User;
import query.db.PaperRepository;
import query.db.QuestionnaireRepository;
import query.db.UserRepository;

/**
 * 管理员行为控制类
 * 
 * @author lzy
 * @version 1.0.4
 * 
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

	/**
	 * 默认一页展示的行数
	 */
	private final static int PAGE = 8;

	/**
	 * 用户数据库操作对象
	 */
	@Autowired
	private UserRepository userRepository;

	/**
	 * 问卷数据库操作对象
	 */
	@Autowired
	private QuestionnaireRepository questionnaireRepository;

	/**
	 * 答卷数据库操作对象
	 */
	@Autowired
	private PaperRepository paperRepository;

	/**
	 * 获取管理员主界面
	 * 
	 * @param session 用户session
	 * @return 管理员主界面
	 */
	@RequestMapping(method = GET)
	public String getPage(HttpSession session) {
		Integer integer = (Integer) session.getAttribute("userPage");
		if (integer == null) {
			integer = new Integer(1);
			session.setAttribute("userPage", integer);
		}
		int page = integer.intValue();
		List<User> users = userRepository.search((page - 1) * PAGE, PAGE);
		session.setAttribute("users", users);
		return "manager";
	}

	/**
	 * 处理用户管理界面分页
	 * 
	 * @param page    要获取的页面的页码
	 * @param session 用户session
	 * @return 操作结果
	 */
	@RequestMapping(value = "/gotoPage", method = POST)
	@ResponseBody
	public String processPage(String page, HttpSession session) {
		int temp;
		try {
			temp = Integer.valueOf(page);
		}catch(Exception e) {
			temp = 1;
		}
		List<User> users = userRepository.search((temp - 1) * PAGE, PAGE);
		if (users.size() != 0) {
			session.setAttribute("userPage", new Integer(temp));
			session.setAttribute("users", users);
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 处理删除用户
	 * 
	 * @param userName 要删除的用户的用户名
	 * @return 重定向用户管理页面
	 */
	@RequestMapping(value = "/deleteUaser/{userName}", method = GET)
	public String processDeleteUser(@PathVariable("userName") String userName) {
		List<Questionnaire> questionnaires = questionnaireRepository.searchQuestionnaires(userName);
		for (int i = 0; i < questionnaires.size(); i++) {
			paperRepository.deletePapers(questionnaires.get(i).getId());
			questionnaireRepository.deleteQuestionnaire(questionnaires.get(i).getId());
		}
		userRepository.delete(userName);
		return "redirect:/manager";
	}

	/**
	 * 处理用户权限变更
	 * 
	 * @param userName 要变更权限的用户名
	 * @param manager  要变更的权限
	 * @return 重定向用户管理页面
	 */
	@RequestMapping(value = "/update/{userName}/{manager}", method = GET)
	public String processUpdateManager(@PathVariable("userName") String userName,
			@PathVariable("manager") int manager) {
		if (manager == 1) {
			userRepository.update(userName, true);
		} else {
			userRepository.update(userName, false);
		}
		return "redirect:/manager";
	}

	/**
	 * 获取问卷管理主页
	 * 
	 * @param session 用户session
	 * @return 问卷管理主页
	 */
	@RequestMapping(value = "/querys", method = GET)
	public String getQueryPage(HttpSession session) {
		Integer integer = (Integer) session.getAttribute("page");
		if (integer == null) {
			integer = new Integer(1);
			session.setAttribute("page", integer);
		}
		int temp = integer.intValue();
		List<Questionnaire> list = questionnaireRepository.searchQuestionnaires((temp - 1) * PAGE, PAGE);
		session.setAttribute("querys", list);
		return "questions";
	}

	/**
	 * 处理问卷页面分页
	 * 
	 * @param page    要获取的页面页码
	 * @param session 用户session
	 * @return 操作结果
	 */
	@RequestMapping(value = "/queryPage", method = POST)
	@ResponseBody
	public String processQueryPage(String page, HttpSession session) {
		int temp;
		try {
			temp = Integer.valueOf(page);
		}catch(Exception e) {
			temp = 1;
		}
		List<Questionnaire> list = questionnaireRepository.searchQuestionnaires((temp - 1) * PAGE, PAGE);
		if (list.size() != 0) {
			session.setAttribute("page", new Integer(temp));
			session.setAttribute("querys", list);
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 修改问卷状态
	 * 
	 * @param id     问卷id
	 * @param status 问卷状态
	 * @return 重定向问卷管理页面
	 */
	@RequestMapping(value = "/updateQuery/{id}/{status}", method = GET)
	public String updateQueryStatus(@PathVariable("id") long id, @PathVariable("status") int status) {
		if (status == 1) {
			questionnaireRepository.updateClose(id, true);
		} else {
			questionnaireRepository.updateClose(id, false);
		}
		return "redirect:/manager/querys";
	}

	/**
	 * 删除问卷及其答卷
	 * 
	 * @param id 问卷id
	 * @return 重定向问卷管理页面
	 */
	@RequestMapping(value = "/deleteQuery/{id}", method = GET)
	public String processDeleteQuery(@PathVariable("id") long id) {
		paperRepository.deletePapers(id);
		questionnaireRepository.deleteQuestionnaire(id);
		return "redirect:/manager/querys";
	}
}
